<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examination extends My_Controller {

    public function __construct()
    {
          parent::__construct();
         $this->load->model('Examination_model', 'examination');
         $this->load->model("News_model","news");
    }

    public function questionList()
    {
        $params['id'] = $this->input->get("id",true) ? $this->input->get("id",true) : 15;
        $validate = new Validate([
            'id' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $fields = " ques.newsID,ques.questionID ,ques.questionTitle,ques.questionType,ques.CreateTime,ques.answer,ques.single_question_point,news.all_question_point";
        $questionList = $this->examination->getQuestionList($params['id'],$fields);
        $userInfo = $this->userDetail;
        $result = ['all_question_point'=>0,'questionList'=>[]];
        foreach ($questionList as $key => $value) {
          $result['all_question_point'] = $value['all_question_point'];
          
          if($userInfo)
          {
    			$questionList[$key]['answerInfo'] = [];
    			$questionList[$key]['answerInfo']['answer'] = $value['answer'];
    			$data = [
    				'newsID'=>$params['id'],
    				'AccountID'=>$userInfo['AccountID'],
    				'questionID'=>$value['questionID']
    			];
    			$questionList[$key]['answerInfo']['statue'] = $this->examination->hasAnswered($data);
			     $questionList[$key]['answerInfo']['isright'] = 0;
            $myAnswer = $this->examination->getMyAnswer($value['questionID'],$userInfo['AccountID']);
            $questionList[$key]['answerInfo']['myAnswer'] = $myAnswer['answer'];
            if($value['questionType'] == 1 || $value['questionType'] == 2){
                if(strpos($value['answer'],$myAnswer['answer']) !== false)
                {
                  $questionList[$key]['answerInfo']['isright'] = 1;
                }
            }elseif ($value['questionType'] == 3 && $myAnswer['answer'] == 1) {
               $questionList[$key]['answerInfo']['isright'] = 1;
            }
            //$questionList[$key]['isright'] = $this->examination->isright($value['questionID'],$value['questionType'],$userInfo['AccountID']);
          }
		  unset($questionList[$key]['isDel'],$questionList[$key]['answer'],$questionList[$key]['all_question_point']);
          if($value['questionType'] == 1)
          {
            $optionList = $this->examination->getOptionList($value['questionID']);
            $questionList[$key]['option'] = $optionList;
          }
          
        }
        $result['questionList'] = $questionList;



        $this->createApi($result);exit;
    }

    public function activityList()
    {
      $this->isArrays = true;
      $params['per_page'] = $this->queryData['per_page'] ? $this->queryData['per_page'] : 1;
      $validate = new Validate([
            'per_page' => 'require|number',
        ]);
      if (!$validate->check($params)) {
          $this->errorCode = 50003;
          $this->errorMessage = $validate->getError();
          $this->createApi();
          exit;
      }
      $limit  = $this->config->item('limit');
      $fields = " questionID, questionTitle, questionType,newsID,CreateTime";
      $questionList = $this->examination->getExaminationList($params['per_page'], $limit, $fields);
      $userInfo = $this->userDetail;
      $params['AccountID'] = $userInfo['AccountID'];
      foreach($questionList as $key=>$value)
      {
        $questionList[$key]['statue'] = 0;
        if($userInfo)
        {
          $questionList[$key]['statue'] = $this->examination->hasAnswered($params);
        }
        
        $newsDetail = $this->news->getNewsDetail($value['newsID'],"cover");
          $questionList[$key]['cover'] = $newsDetail['cover']?OSSHOST.$newsDetail['cover']:'';
      }
      $this->createApi($questionList);exit;
    }

    public function answer()
    {
      $params['newsID'] = $this->queryData['newsID'] ? $this->queryData['newsID'] : 18;
		  $params['questionID'] = $this->queryData['questionID'] ? $this->queryData['questionID'] : 53;
		  $params['answer'] = $this->queryData['answer'] ? $this->queryData['answer'] :'3';
      $validate = new Validate([
		      'questionID' => 'require|number',
          'answer' => 'require|string',
          'newsID' => 'require|number',
      ]);
      if (!$validate->check($params)) {
          $this->errorCode = 50003;
          $this->errorMessage = $validate->getError();
          $this->createApi();
          exit;
      }

      $params['AccountID'] = $this->userDetail['AccountID'];
		//查询题目是否存在
		    $questionExist = $this->examination->checkQuestionExist($params['questionID']);
        $answer_arr = json_decode($answer_list,true);
         //查询所有题目
         $fields = " ques.newsID,ques.questionID ,ques.questionTitle,ques.questionType,ques.CreateTime,ques.answer,ques.single_question_point,news.all_question_point";
        $questionList = $this->examination->getQuestionList($params['newsID'],$fields);
        foreach ($questionList as $qkey=> $ques) {
          if($ques['questionID'] != $answer_arr[$qkey]['questionID'] && ($ques['questionType'] != $answer_arr[$qkey]['questionType']))
          {
              $this->errorCode = 80003;
              $this->createApi();exit;
          }
        }
        foreach ($answer_arr as $key => $value) {
          $params['questionID'] = $value['questionID'];
          $quesExist = $this->examination->getQuestionById($value['questionID'],$value['questionType']);
          if(!$quesExist)
          {
            $this->errorCode = 80002;
            $this->createApi();exit;
          }
          $hasAnswered = $this->examination->hasAnswered($params);
          if($hasAnswered)
          {
            $this->errorCode = 80001;
            $this->createApi();exit;    
          }
        }
        foreach ($answer_arr as $key => $value) 
        {
          $params['questionID'] = $value['questionID'];
          $params['answer'] = $value['answer'];
          $params['newsID'] = $params['newsID'];
          $params['Createtime'] = date("Y-m-d H:i:s",time());
          $answerID = $this->examination->answer($params);
          //单选填空添加积分
          if($answerID)
          {
            if($value['questionType'] == 1 || $value['questionType'] == 2){
                if(strpos($value['answer'],$params['answer']) !== false)
                {
                  //添加积分
                  $pointData = [
                    'points'=>1,
                    'AccountID'=>$params['AccountID'],
                    'type'=>4,
                    'createtime'=>date("Y-m-d",time()),
                    'typeid'=>$value['questionID'],
                  ];
                  $this->user->addAnswerPoints($pointData);
                }
            }
          }
        }
        $this->createApi();
    }
}

/* End of file region.php */
/* Location: ./application/controllers/region.php */
